package br.edu.ifrn.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.edu.ifrn.beans.Convenio;
import br.edu.ifrn.persistencia.ConvenioDAO;


@SuppressWarnings("serial")
public class ConvenioServlet extends HttpServlet {
	private HttpSession sessao;
	private ConvenioDAO convenioNoBanco = new ConvenioDAO();

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		sessao = req.getSession();
		//sessao.removeAttribute("erroConvenio");
		addConvenio(req, resp);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//		sessao.removeAttribute("mensagem");
		sessao = request.getSession();
		logar(request, response);
	}

	public void addConvenio(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		RequestDispatcher jsp;
			sessao.removeAttribute("erroConvenio");
		if (req.getParameter("senha1").equals(req.getParameter("senha2"))) {
			Convenio novoConvenio = new Convenio();

			novoConvenio.setCNPJ(req.getParameter("cnpj"));
			novoConvenio.setNome(req.getParameter("nome"));
			novoConvenio.setNomeFantasia(req.getParameter("nomeFantasia"));
			novoConvenio.setLogradouro(req.getParameter("logradouro"));
			novoConvenio.setNumero(req.getParameter("numero"));
			novoConvenio.setBairro(req.getParameter("bairro"));
			novoConvenio.setCep(req.getParameter("cep"));
			novoConvenio.setCidade(req.getParameter("cidade"));
			novoConvenio.setUF(req.getParameter("uf"));
			novoConvenio.setComplemento(req.getParameter("complemento"));
			novoConvenio.setTelefone(req.getParameter("telefone"));
			novoConvenio.setEmail(req.getParameter("email"));
			novoConvenio.setRepresentante(req.getParameter("representante"));
			novoConvenio.setCpfRepresentante(req.getParameter("cpfRepresentante"));
			novoConvenio.setSenha(req.getParameter("senha1"));

			convenioNoBanco.inserirConvenio(novoConvenio);
			sessao.setAttribute("convenio", novoConvenio);

			String mensagem = "Cadastro realizado com sucesso";
			sessao.setAttribute("mensagem", mensagem);

			jsp = req.getRequestDispatcher("convenios.jsp");
			jsp.forward(req, resp);

		} else {
			String erroConvenio = "As senhas digitadas foram diferentes";
			sessao.setAttribute("erroConvenio", erroConvenio);
			jsp = req.getRequestDispatcher("novoconvenio.jsp");
			jsp.forward(req, resp);
		}
	}

	private void logar(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		RequestDispatcher jsp;
		Convenio convenio = convenioNoBanco.logar(req.getParameter("cnpj"), req.getParameter("senha1"));
		
		if (convenio == null) {
			String erroConvenio = "Usuario Invalido";
			sessao.setAttribute("erroConvenio", erroConvenio);
			jsp = req.getRequestDispatcher("convenio.jsp");
			jsp.forward(req, resp);
		} else {
			sessao.removeAttribute("erroConvenio");
			sessao.setAttribute("convenio", convenio);
			String mensagem = "Bem-vindo";
			sessao.setAttribute("mensagem", mensagem);
			jsp = req.getRequestDispatcher("convenios.jsp");
			jsp.forward(req, resp);
		}
	}

}
